<template>
  <div class="box">
    <div class="box1">
      <p class="p1">早上好，用户</p>
      <p class="p2">
        <span
          ><img src="/401位置.png" alt="" width="20rem" height="20rem"
        /></span>
        <span style="margin-left: 0.5rem">河北省保定市</span>
      </p>
      <div class="box2">
        <van-icon name="chat-o" dot @click="router.push('/tz')" />
      </div>
      <input
        type="text"
        class="int1"
        placeholder="搜索"
        @focus="router.push('/ss')"
      />
    </div>
    <div class="box3">
      <el-carousel :interval="2000" type="card" height="11rem" width="100%">
        <el-carousel-item
          v-for="item in listr"
          :key="item.id"
          style="width: 90%; margin: 0 -4.5rem"
        >
          <div
            style="width: 100%; height: 100%; box-shadow: 0 0 10px #ccc"
            class="box4"
          >
            <p class="p1">50折优惠</p>
            <span class="p3">30%</span>
            <p class="p2">今天的特别优惠</p>
            <p class="p4">每个服务订单均可享受折扣！</p>
            <p class="p5">仅当日有效！</p>
          </div>
        </el-carousel-item>
      </el-carousel>
    </div>
    <div class="box5">
      <span class="sp1" @click="show('理发')">
        <p class="p1">
          <i
            class="iconfont icon-noun__cc"
            style="
              font-size: 1.5rem;
              position: relative;
              top: 0.9rem;
              color: rgb(255, 185, 0);
            "
          ></i>
        </p>
        <p style="margin-top: 0.5rem">理发</p>
      </span>
      <span class="sp1" @click="show('染色')">
        <p class="p1">
          <i
            class="iconfont icon-ranfaji"
            style="
              font-size: 1.5rem;
              position: relative;
              top: 0.7rem;
              color: rgb(255, 185, 0);
            "
          ></i>
        </p>
        <p style="margin-top: 0.5rem">染色</p>
      </span>

      <span class="sp1" @click="show('洗发')">
        <p class="p1">
          <i
            class="iconfont icon-linyu"
            style="
              font-size: 2.1rem;
              position: relative;
              top: 0.2rem;
              color: rgb(255, 185, 0);
            "
          ></i>
        </p>
        <p style="margin-top: 0.5rem">洗发</p>
      </span>
      <span class="sp1">
        <p class="p1">
          <i
            class="iconfont icon-gengduo"
            style="
              font-size: 2rem;
              position: relative;
              top: 0.5rem;
              color: rgb(255, 185, 0);
            "
          ></i>
        </p>
        <p style="margin-top: 0.5rem">更多</p>
      </span>
    </div>
    <div class="box6">
      <p>
        <span style="font-size: 1.5rem; font-weight: 600; margin-left: 1rem"
          >待使用</span
        >
        <span
          style="
            float: right;
            color: rgb(255, 185, 0);
            margin-right: 1rem;
            margin-top: 0.5rem;
          "
          @click="router.push('/cut')"
          >查看全部</span
        >
      </p>
      <div class="box7">
        <p class="p1">
          <span>2024年 8月 28日</span>
          <span>
            <span style="margin-right: 0.5rem; color: rgb(206, 208, 210)"
              >提醒我</span
            >
            <span style="float: right; margin-top: 0.2rem"
              ><van-switch
                v-model="checked"
                active-color="#FFA500"
                inactive-color="#dcdee0"
                size="20px"
            /></span>
          </span>
        </p>
        <div v-for="item in orderList.slice(0,1)" :key="item._id">

        
        <div class="box8" >
          <div class="box9">
            <img
              src="https://tse1-mm.cn.bing.net/th/id/OIP-C.oouRu_203f--LLj5l2PiSQHaHa?w=171&h=180&c=7&r=0&o=5&dpr=1.3&pid=1.7"
              alt=""
              width="100%"
              height="100%"
            />
          </div>
          <div class="box10">
            <p style="font-size: 1.2rem; font-weight: 600; margin-top: 0.2rem">
              {{item.salonId.name}}
            </p>
            <p style="font-size: 0.8rem">{{item.salonId.address}}</p>
            <p style="margin-top: 1.5rem; font-size: 0.8rem">服务:</p>
            <p style="font-size: 0.8rem">{{ item?.serviceId?.name || item?.packageId?.name }}</p>
          </div>
        </div>
        <div class="box11">
          <button class="btn1" @click="quxiao(item)">取消预约</button>
          <button class="btn2" @click="chanstatus(item)">立即使用</button>
        </div>
        </div>
      </div>
    </div>
    <div class="box12">
      <span style="font-size: 1.2rem">附近门店</span>
      <span
        style="float: right; color: rgb(255, 185, 0); margin-top: 0.2rem"
        @click="router.push('/fjmd')"
        >查看全部</span
      >
      <p style="margin-top: 1rem">
        <button
          v-for="i in type"
          :key="i"
          :class="typer == i ? 'btn2' : 'btn1'"
          @click="(typer = i), getlist()"
        >
          {{ i }}
        </button>
      </p>
      <div
        class="box13"
        v-for="i in list.slice(0, 3)"
        :key="i"
        @click="shopfrontinfo(i)"
      >
        <div class="box14">
          <img
            src="https://tse2-mm.cn.bing.net/th/id/OIP-C.8InsvK94GQ2nhzRViLbJWAHaE8?w=278&h=185&c=7&r=0&o=5&dpr=1.3&pid=1.7"
            alt=""
          />
        </div>
        <div class="box15">
          <p style="font-weight: 600; font-size: 17px">
            {{ i.name.slice(0, 10) + '...' }}
          </p>
          <p
            style="
              margin-top: 0.5rem;
              font-size: 14px;
              color: rgb(126, 126, 126);
            "
          >
            {{ i.postton.slice(0, 20) + '...' }}
          </p>
          <p style="margin-top: 0.5rem">
            <span
              ><van-icon name="location" color="orange" />
              {{ i.gl + '公里' }}</span
            >
            <span style="margin-left: 0.8rem"
              ><van-icon name="star" color="orange" /> {{ i.pj }}</span
            >
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import axios from 'axios';
import { computed, defineComponent, ref, reactive, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import {
  showConfirmDialog,
  showSuccessToast,
  showFailToast,
  showLoadingToast,
  showToast,
} from "vant";
const router = useRouter();

const checked = ref(true);
const listr = reactive([{ id: 1 }, { id: 2 }, { id: 3 }]);
const type = reactive(['全部', '理发', '护发', '染发']);
const typer = ref('全部');
const list = reactive([]);
axios.defaults.baseURL = 'http://127.0.0.1:3000/shopfront';
const orderList = ref([]);
const page = ref(1); // 当前页数
const pagesize = ref(20);
const getorder = async () => {
  let {
    data: { data },
  } = await axios.get(
    `/getorder?page=${page.value}&pagesize=${pagesize.value}`,
  );
  console.log(data);
  
  orderList.value = data.filter((i) => i.status == 0)
};
const show = (i) => {
  router.push({
    path: '/show',
    query: {
      type: i,
    },
  });
};
const getlist = () => {
  axios
    .get('http://localhost:3000/jsb/gettype?type=' + typer.value)
    .then((res) => {
      list.length = 0;
      list.push(...res.data.list);
      console.log('====================================');
      console.log(list);
      console.log('====================================');
    });
};
const shopfrontinfo = (i) => {
  router.push({
    name: 'info',
    query: {
      data:JSON.stringify(i),
    },
  });
};
onMounted(() => {
  getlist();
  getorder();
});
const quxiao = (i) => {
  console.log(i);
  
  let obj = {
    refund_amount: i.amout, // 确保属性名正确
    trade_no: i.trade,
  };
  showConfirmDialog({
    title: "取消订阅",
    message: "确定取消订阅吗？",
  })
    .then(() => {
      axios
        .post("/api/refund", obj)
        .then((response) => {
          if (response.data.code === 200) {
            console.log("退款请求成功", response.data.result);
            // 更新订单状态为已退款
            axios
              .put(`/chanrefund?_id=${i._id}`)
              .then((res) => {
                if (res.data.code === 200) {
                  showSuccessToast("退款成功！");
                  getorder();
                }
              })
              .catch((error) => {
                showFailToast("退款失败");
              });
          } else {
            console.error("退款请求失败", response.data);
          }
        })
        .catch((error) => {
          console.error("发送退款请求失败", error);
        });
    })
    .catch(() => {
      console.log("用户取消了操作");
    });
};

// 改变立即使用
const chanstatus = async (info) => {
  if(info.remind){
    showToast('已提醒商家确认,请勿重复操作');
    return;
  }
  showConfirmDialog({
    title: "使用",
    message: "确定使用吗？"
  })
    .then(() => {
      chanremind(info._id)
      showToast("已提醒商家确认");
    })
    .catch(() => {
      // on cancel
      showToast('取消使用')
    });
};

</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
}
.box {
  width: 100vw;
}
.box1 {
  width: 100vw;
  height: 11rem;
  background-color: rgb(255, 255, 255);
  overflow: hidden;
  .p1 {
    color: rgb(0, 0, 0);
    font-size: 1.2rem;
    margin-top: 1rem;
    margin-left: 1rem;
    font-weight: 600;
  }
  .p2 {
    color: rgb(0, 0, 0);
    font-size: 1rem;
    margin-left: 1rem;
    margin-top: 0.5rem;
  }
}
.icon {
  width: 22px;
  height: 22px;
  margin-top: 3px;
  // vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
}

.box2 {
  width: 3rem;
  height: 3rem;
  background-color: rgb(240, 240, 240);
  color: rgb(0, 0, 0);
  border-radius: 50%;
  text-align: center;
  font-size: 1.5rem;
  line-height: 3rem;
  position: absolute;
  top: 1.2rem;
  right: 1rem;
}
.int1 {
  width: 17rem;
  margin-top: 2rem;
  height: 3rem;
  background-color: rgb(255, 255, 255);
  line-height: 3rem;
  border-radius: 1rem;
  color: white;
  border: none;
  margin-left: 1.1rem;
  border: 1px solid rgb(212, 212, 212);
  background: url('/搜索.png') no-repeat left center;
  padding-left: 70px; /* 根据图片大小调整 */
  background-size: 40px;
  background-position-x: 10px;
}
.box3 {
  margin-top: 1rem;
  width: 100%;
  height: 11rem;
  // background-color: pink;
}

.box4 {
  background-color: rgb(255, 185, 0);
  border-radius: 10px;
  overflow: hidden;
  .p1 {
    margin-top: 1rem;
    margin-left: 1rem;
  }
  .p2 {
    margin-top: 0.3rem;
    margin-left: 1rem;
    font-size: 20px;
    font-weight: 600;
  }
  .p3 {
    float: right;
    margin-right: 1rem;
    font-size: 30px;
  }
  .p4 {
    margin-top: 2rem;
    margin-left: 1rem;
  }
  .p5 {
    margin-left: 1rem;
  }
}
.box5 {
  width: 100vw;
  height: 6rem;
  margin-top: 1rem;
  display: flex;
  justify-content: space-around;
  .sp1 {
    margin-top: 0.5rem;
    text-align: center;
  }
  .p1 {
    width: 3rem;
    height: 3rem;
    background-color: rgb(255, 248, 230);
    border-radius: 50%;
  }
}
.box6 {
  width: 100vw;
  height: 26rem;
  margin-top: 2rem;
}
.box7 {
  width: 88vw;
  height: 17rem;
  margin: 0 auto;
  margin-top: 1rem;
  border-radius: 10px;
  border: 1px solid black;
  .p1 {
    width: 90%;
    height: 2rem;
    margin: 0 auto;
    line-height: 2rem;
    display: flex;
    justify-content: space-between;
    border-bottom: 2px solid rgb(206, 208, 210);
  }
}
.box8 {
  width: 90%;
  height: 8rem;
  margin: 0 auto;
  margin-top: 1rem;
  display: flex;
  justify-content: space-between;
}
.box9 {
  width: 30%;
  height: 100%;
  img {
    border-radius: 10px;
  }
}
.box10 {
  width: 65%;
  height: 100%;
}
.box11 {
  width: 90%;
  height: 3rem;
  margin: 0 auto;
  margin-top: 1rem;
  display: flex;
  justify-content: space-between;
  .btn1 {
    width: 49%;
    height: 100%;
    border-radius: 10px;
    border: 1px solid rgb(255, 185, 0);
    background-color: white;
    color: rgb(255, 185, 0);
  }
  .btn2 {
    width: 49%;
    height: 100%;
    border-radius: 10px;
    border: none;
    background-color: rgb(255, 185, 0);
    color: white;
  }
}
.box12 {
  width: 21rem;
  height: 35rem;
  margin: 0 auto;

  .btn1 {
    margin-right: 2rem;
    padding: 0.4rem 0.6rem;
    border: none;
    border-radius: 10px;
  }
  .btn2 {
    margin-right: 2rem;
    padding: 0.4rem 0.6rem;
    border: none;
    border-radius: 10px;
    background-color: orange;
  }
}
.box13 {
  display: flex;
  width: 21rem;
  height: 7rem;
  margin: 2rem auto;
  justify-content: space-between;
  background-color: white;
}
.box14 {
  width: 30%;
  height: 80%;
  margin-top: 0.7rem;
  margin-left: 0.5rem;
  img {
    width: 100%;
    height: 100%;
    border-radius: 10px;
  }
}
.box15 {
  width: 60%;
  height: 80%;
  margin-top: 0.7rem;
}
</style>